package de.hhn.mim.psb;


/**
 * Klasse definiert einen zweidimensionalen Vektor mit den Elementen
 * x und y und einige wichtige Vektoroperationen 
 * Addition
 * Subtraktion
 * norm und normalize (a: berechnet den Betrag, b: normiert Komponenten)
 * Skalar/Dot-Produkt
 * Kreuz-Produkt 
 * 
 * @author Rolf Bendl
 * 
 * 2012-05-12: Klasse erstellt
 */
public class Vect2D
 {
  double x;
  double y;
  
  public Vect2D() {x = 0; y = 0; }
  public Vect2D(double x, double y) 
   {
    this.x = x;
    this.y = y;
   }
  public Vect2D addVect(Vect2D v)
   {
    Vect2D result = new Vect2D();
    result.x = x + v.x;
    result.y = y + v.y;
    return result;
   }
  
  public Vect2D subtractVect(Vect2D v)
   {
    Vect2D result = new Vect2D();
    result.x = x - v.x;
    result.y = y - v.y;
    return result;
   }
  
  public double norm()
   {
    return Math.sqrt(x*x + y*y);
   }
  
  public Vect2D normalize()
   {
    Vect2D result = new Vect2D();
    double length = norm();
    result.x = x / length;
    result.y = y / length;
    return result;
   }
  public double dotProduct(Vect2D v)
   {
    return scalarProduct(v);
   }
  public double scalarProduct(Vect2D v)
   {
    double result = 0;
    result = x * v.x + y * v.y;
    return result;    
   }
  public double crossProduct(Vect2D v)
   {
    double result = 0;
    result = x * v.y - y * v.x;  
    return result;    
   }
 }

